home *** CD-ROM | disk | FTP | other *** search
/ Experimental BBS Explossion 3 / Experimental BBS Explossion III.iso / c / cp1.zip / LOOKUP.CH < prev    next >
Text File  |  1993-05-12  |  4KB  |  126 lines

  1. ===========================================================================
  2.  BBS: The Abacus * HST/DS * Potterville MI
  3. Date: 05-09-93 (14:22)             Number: 31
  4. From: MARK CORGAN                  Refer#: NONE
  5.   To: ALL                           Recvd: NO  
  6. Subj: LOOKUP.C & DEFINES.H           Conf: (36) C Language
  7. ---------------------------------------------------------------------------
  8. --->CONTINUED FROM PREVIOUS MESSAGE<---
  9.  
  10. /* DEFINES.H */
  11.  
  12. #define MAX_LINE           40     /* maximum line length */
  13. #define MAX_KEY            26     /* maximum key size    */
  14. #define ASCII_FILE  "info.dat"    /* the data base       */
  15. #define INDEX_FILE  "info.ndx"    /* the index file      */
  16. #define END_REC         ".\n"     /* end of a record     */
  17. #define TRUE                1     /* for BOOLEAN         */
  18. #define FALSE               0
  19.  
  20. typedef char BOOLEAN;             /* data type */
  21.  
  22. struct index_rec                  /* index record */
  23. {
  24.    char key[MAX_KEY];             /* name */
  25.    long pos;                      /* position */
  26. };
  27.  
  28. typedef struct index_rec INDEX;
  29.  
  30. /* LOOKUP.C */
  31.  
  32. #include <stdio.h>
  33. #include "index.h"
  34.  
  35. long bsearch(FILE *ifp, long first, long last, char *target);
  36. FILE *my_fopen(char *file_name, char *mode);
  37.  
  38. void main(void)
  39. {
  40.    FILE *afp, *ifp;
  41.    char line[MAX_LINE];
  42.    INDEX header;
  43.    long pos;
  44.  
  45.    afp = my_fopen(ASCII_FILE, "r");
  46.    ifp = my_fopen(INDEX_FILE, "rb");
  47.    if(fread((char *) &header, sizeof(INDEX), 1, ifp) == 0)
  48.       fprintf(stderr, "Can't read header of \"%s\"\n", INDEX_FILE);
  49.    else
  50.       while(printf("Name? "), fgets(line, sizeof(line), stdin))
  51.          if((pos = bsearch(ifp, 1L, header.pos, line)) == -1)
  52.             printf("Couldn't find: %s\n", line);
  53.          else if(fseek(afp, pos, 0) != 0)
  54.             fprintf(stderr, "Can't read record at position %ld\n", pos);
  55.          else
  56.             while(fgets(line,sizeof(line),afp) && strcmp(line,END_REC)!=0)
  57.                fputs(line, stdout);
  58.    fclose(ifp);
  59.    fclose(afp);
  60.    exit(0);
  61. }
  62.  
  63. long bsearch(FILE *ifp, long first, long last, char *target)
  64. {
  65.    long pos, mid =(first + last) / 2;
  66.    INDEX next;
  67.    int cmp;
  68.  
  69.    if(mid < first || fseek(ifp, mid * sizeof(INDEX), 0) != 0 ||
  70.             fread((char *) &next, sizeof(INDEX), 1, ifp) == 0)
  71.       pos = -1;
  72.    else
  73.       pos = ((cmp = strncmp(target, next.key, MAX_KEY)) == 0)
  74.                 ? next.pos
  75.                 : ((cmp < 0) ? bsearch(ifp, first, mid - 1, target)
  76.                              : bsearch(ifp, mid + 1, last, target));
  77.    return pos;
  78. }
  79.  
  80. FILE *my_fopen(char *file_name, char *mode)
  81. {
  82.    FILE *fp = fopen(file_name, mode);
  83.  
  84.    if(!fp)
  85.    {
  86.       fprintf(stderr, "Can't open file \"%s\" for %s\n", file_name,
  87.                       (*mode == 'r')
  88.                            ? "reading"
  89.                            : ((*mode == 'w') ? "writing" : "appending"));
  90.       exit(1);
  91.    }
  92.    return fp;
  93. }
  94.  
  95. /* end of LOOKUP.C */
  96.  
  97. The ASCII text file's format is as follows:
  98.  
  99. Mark Corgan
  100. 550 Foothill Rd.
  101. Gardnerville, NV 89410
  102. (702) 265-2388
  103. .
  104. Hello World
  105. 123 Anywhere St.
  106. Anytown, CA 12345
  107. (123) 456-7890
  108. .
  109. etc...
  110.  
  111. The period is what LOOKUP.C looks for to indicate the end of record. Of course,
  112. you could have any format you like, so long as the first line is the information
  113.  you are looking for. Also, there is no limit to the number of lines of infomati
  114. on after the first line and before the period as fputs() continues until the per
  115. iod. Enjoy!
  116.  
  117.  
  118.  
  119. Mark
  120.  
  121. --- GoldED 2.40
  122.  * Origin: -*- Sound and Image -*-  (1:213/810)
  123. SEEN-BY: 1/211 11/2 4 13/13 101/1 108/89 109/25 110/69 114/5 123/19 124/1
  124. SEEN-BY: 153/752 154/40 77 157/2 159/100 125 575 950 203/23 209/209 261/1023
  125. SEEN-BY: 280/1 390/1 396/1 5 15 2270/1 2440/5 3603/20
  126.